package c.x.jy.service.service.impl;

import c.x.jy.common.annotation.DelCache;
import c.x.jy.dao.entity.BusinessConfigEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import c.x.jy.common.utils.PageUtils;
import c.x.jy.common.utils.Query;

import c.x.jy.dao.dao.SysUserRoleDao;
import c.x.jy.dao.entity.SysUserRoleEntity;
import c.x.jy.service.service.SysUserRoleService;
import org.apache.commons.lang3.StringUtils;


@Service("sysUserRoleService")
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleDao, SysUserRoleEntity> implements SysUserRoleService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        	QueryWrapper<SysUserRoleEntity> query= 	 new QueryWrapper<SysUserRoleEntity>() ;
    	if(params.get("searchKey")!=null&&params.get("searchValue")!=null&&!StringUtils.isEmpty(params.get("searchKey").toString())&&!StringUtils.isEmpty(params.get("searchValue").toString()))
    	    query.eq(  params.get("searchKey").toString(), params.get("searchValue"));
 
        IPage<SysUserRoleEntity> page = this.page(
                new Query<SysUserRoleEntity>().getPage(params),
                 query
        );

        return new PageUtils(page);
    }

    @DelCache("user")
    @Override
    public int setRoleUser(Long userId, List<String> roleList) {
        QueryWrapper<SysUserRoleEntity> wrapper = new QueryWrapper<SysUserRoleEntity>();
        wrapper.eq("user_id", userId);
        this.baseMapper.delete(wrapper);
        List<SysUserRoleEntity> list=new ArrayList<>();
        for (String roleId : roleList) {
            if (StringUtils.isEmpty(roleId))  continue;
            SysUserRoleEntity entity = new SysUserRoleEntity();
            entity.setRoleId(Long.valueOf(roleId));
            entity.setUserId(userId);
            list.add(entity);
        }

        this.baseMapper.insertbatths(list);
     return 0;
    }

}
